#!/usr/bin/env python2.7
# _*_ coding:UTF-8 _*_
"""
__author__ = 'shede333'
"""

from calibre.web.feeds.recipes import BasicNewsRecipe
from calibre.ebooks import BeautifulSoup
# from bs4 import BeautifulSoup

class ProGitBook(BasicNewsRecipe):
    title = 'Pro Git 2nd Edition'
    description = ''
    cover_url = 'https://git-scm.com/images/progit2.png'

    url_prefix = 'https://git-scm.com/book/zh/v2'
    # no_stylesheets = True

    # keep_only_tags = [{ 'class': 'span-21' }]

    def parse_index(self):
        print "sw-parse_index"
        print BeautifulSoup, type(BeautifulSoup)
        print BeautifulSoup.__version__

        soup = self.index_to_soup(self.url_prefix)
        print type(soup)
        html_volume_list = soup.findAll('li', "chapter")
        # print "html_volume_list: ", html_volume_list
        volume_list = []
        for html_volume in html_volume_list:
            # print "html_volume:", html_volume
            html_volume_title = html_volume.h2
            html_volume_chapter = html_volume.ol
            print "html_volume_title.strings:", html_volume_title
            volume_title = unicode(html_volume_title.contents[0]).strip() + u" " + unicode(html_volume_title.a.string).strip()
            chapter_list = []
            for html_chapter in html_volume_chapter.findAll("li"):
                chapter_title = unicode(html_chapter.contents[0]).strip() + u" " + unicode(html_chapter.a.string).strip()
                chapter_url = u"https://git-scm.com" + html_chapter.a["href"]
                chapter_list.append({"title": chapter_title, "url": chapter_url})

            volume_list.append((volume_title, chapter_list))
        print volume_list
        return volume_list
